package com.csiec.clock.view.model;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.DialogInterface;
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.csiec.clock.adpter.ClockInfoAdapter;
import com.csiec.clock.adpter.MyClockAdapter;
import com.csiec.clock.bean.MyClock;
import com.csiec.clock.bean.people.People;
import com.csiec.clock.bean.people.ResultCode;
import com.csiec.clock.callback.ClockInfoCallBack;
import com.csiec.clock.databinding.ActivityClockInfoBinding;
import com.csiec.clock.model.ClockModel;
import com.csiec.clock.utils.FileUtil;
import com.csiec.clock.utils.TimeGet;
import com.github.gzuliyujiang.calendarpicker.CalendarPicker;
import com.qmuiteam.qmui.widget.QMUIProgressBar;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class ClockInfoViewModel extends ViewModel {

    private final String TAG = ClockInfoViewModel.class.getSimpleName();

    @SuppressLint("StaticFieldLeak")
    private Activity activity;
    private ActivityClockInfoBinding binding;
    private MyClock ownClock;
    long first_time = 0;
    private MutableLiveData<String> timeStart;
    private MutableLiveData<String> complete;
    @SuppressLint("SimpleDateFormat")
    SimpleDateFormat manyFormatter = new SimpleDateFormat("yyyy-MM-dd");

    private ClockModel model;
    private final android.os.Handler handler = new Handler();
    private List<People> peopleList;

    public void init(Activity activity, ActivityClockInfoBinding binding, MyClock ownClock) {
        this.activity = activity;
        this.binding = binding;
        this.ownClock = ownClock;
        model = new ClockModel();

        if (timeStart == null) {
            String format = manyFormatter.format(new Date());
            timeStart = new MutableLiveData<>();
            timeStart.setValue(format);
        }
        if (complete == null) {
            complete = new MutableLiveData<>();
        }
        getClockInfo();

    }

    public void getClockInfo() {
        //获取打卡id
        //获取选择的时|间
        model.getIClockDataUrl(ownClock.getId(), timeStart.getValue(), new ClockInfoCallBack() {
            @Override
            public void onSuccess(ResultCode backData) {
                if (backData.getStatus() == 200) {
                    peopleList = backData.getData().getData();
                    handler.post(() -> {
                        setProgressBar(backData.getData().getCount(), backData.getData().getCompleted());
                        initView();
                    });
                }
            }

            @Override
            public void onFailed() {
                handler.post(() -> {
                    showTip(QMUITipDialog.Builder.ICON_TYPE_FAIL, "网络异常");
                });
            }
        });

    }

    //填充数据
    private void initView() {
        binding.recyclerView2.setLayoutManager(new LinearLayoutManager(activity.getApplication()));
        if (peopleList != null) {
            ClockInfoAdapter adapter = new ClockInfoAdapter(peopleList, activity);
            binding.recyclerView2.setAdapter(adapter);
        }
    }


    //设置图像
    private void setProgressBar(int max, int progress) {
        binding.progressBar.setMaxValue(max);
        binding.progressBar.setProgress(progress);
        if (progress == 0) {
            complete.setValue("完成率 0%");
        } else {
            complete.setValue("完成率");
        }
        binding.progressBar.setQMUIProgressBarTextGenerator((progressBar, value, maxValue) -> 100 * value / maxValue + "%");
        binding.progressBar1.setMaxValue(100);
        binding.progressBar1.setProgress(100);
        binding.progressBar2.setMaxValue(100);
        binding.progressBar2.setProgress(100);
        binding.progressBar1.setQMUIProgressBarTextGenerator((progressBar, value, maxValue) -> "已打卡");
        binding.progressBar2.setQMUIProgressBarTextGenerator((progressBar, value, maxValue) -> "未打卡");
    }

    /**
     * 复制到剪贴板
     */
    public void copyText() {
        FileUtil.copyText(activity, ownClock.getSharingCode());
        showTip(QMUITipDialog.Builder.ICON_TYPE_SUCCESS, "已将分享码复制到剪贴板");
    }

    //选择时间
    public void selectTime() {
        CalendarPicker picker = new CalendarPicker(activity);
        picker.setOnSingleDatePickListener(date -> {
            first_time = date.getTime();
            timeStart.setValue(TimeGet.date2String(first_time));
            getClockInfo();
        });

        //将可选日期设置为打卡的开始到结束时间
        Date startTime;
        Date endTime;
        //判断打卡状态{"进行中", "已结束", "未开始"};
        switch (ownClock.getState()) {
            case 0:
                //开始时间到当天
                //判断打卡类型
                if (MyClock.CLOCKTYPE.equals(ownClock.getType())) {
                    //单次打卡
                    startTime = TimeGet.string2Date(ownClock.getSingleStartTime(), ownClock.getType());
                } else {
                    startTime = TimeGet.string2Date(ownClock.getManyStartDate(), ownClock.getType());
                }
                endTime = new Date();
                break;
            case 1:
//                开始时间到结束时间
                //判断打卡类型
                if (MyClock.CLOCKTYPE.equals(ownClock.getType())) {
                    //单次打卡
                    startTime = TimeGet.string2Date(ownClock.getSingleStartTime(), ownClock.getType());
                    endTime = TimeGet.string2Date(ownClock.getSingleEndTime(), ownClock.getType());
                } else {
                    startTime = TimeGet.string2Date(ownClock.getManyStartDate(), ownClock.getType());
                    endTime = TimeGet.string2Date(ownClock.getManyEndDate(), ownClock.getType());
                }
                break;
            default:
                //只能选择当天
                startTime = new Date();
                endTime = new Date();
                break;
        }
        picker.setRangeDate(startTime, endTime);
        picker.show();
    }

    //提示悬浮窗
    public void showTip(int type, String msg) {
        QMUITipDialog tipDialog = new QMUITipDialog.Builder(activity)
                .setIconType(type)
                .setTipWord(msg)
                .create();
        tipDialog.show();
        binding.textView2.postDelayed(tipDialog::dismiss, 2000);
    }

    public MutableLiveData<String> getComplete() {
        return complete;
    }

    public MutableLiveData<String> getTimeStart() {
        return timeStart;
    }
}
